package com.example.park_management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.park_management.entity.Sebei;
import com.example.park_management.result.R;
import com.example.park_management.service.SebeiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("sebei")
public class sebeiController {
    @Autowired
    SebeiService sebeiService;

    // 分页查询 Page  传递参数有 page pageSize 设备名称
    @RequestMapping("/look")
    public Page look(int page ,int pageSize,String seName){
        System.out.println("page为"+page);
        Page<Sebei> pageinfo = new Page<>(page,pageSize);
        //        构造条件构造器
        LambdaQueryWrapper<Sebei> queryWrapper = new LambdaQueryWrapper();
//        添加过滤条件
        queryWrapper.like(seName !=null,Sebei::getSeName,seName);
//        执行条件
        sebeiService.page(pageinfo,queryWrapper);
        return pageinfo;
    }

//
    @RequestMapping("update")
    public R<String> update(Sebei sebei) {
        Boolean a = sebeiService.updateById(sebei);
        if (a){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
    @RequestMapping("add")
    public R<String> add(@RequestBody Sebei sebei){
        System.out.println(sebei.getSeName());
        sebei.setStatus(1);
        Boolean a =  sebeiService.save(sebei);
        if (a){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }
    @RequestMapping("delete")
    public R<String> delete(@RequestBody Sebei sebei){
        System.out.println(sebei.getId());
        Boolean a =  sebeiService.removeById(sebei.getId());
        if (a){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

}
